package com.webside.playbackstatistics.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.base.baseservice.impl.AbstractService;
import com.webside.playbackstatistics.mapper.AnaSummaryStatMapper;
import com.webside.playbackstatistics.model.AnaSummaryStatEntity;
import com.webside.playbackstatistics.model.SeriesStatEntity;
import com.webside.playbackstatistics.service.AnaSummaryStatService;
import com.webside.playbackstatistics.service.PlayStatisticsService;
import com.webside.playbackstatistics.service.SeriesStatService;
import com.webside.resource.model.ResourceEntity;

@Service("AnaSummaryStatService")
public class AnaSummaryStatServiceImpl extends AbstractService<ResourceEntity, Long> implements AnaSummaryStatService {
	@Autowired
	private SeriesStatService seriesStatService;
	@Autowired
	private PlayStatisticsService playStatisticsService;
	@Autowired
	private AnaSummaryStatMapper anaSummaryStatMapper;

	// 这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(anaSummaryStatMapper);
	}

	// 统计截至昨天的所有剧集的日，周，总，和上升指数
	@Override
	public List<AnaSummaryStatEntity> statAnaSummaryList() {
		// 将查到的昨天所有剧集的各类信息放入anaSummaryList集合并返回
		List<AnaSummaryStatEntity> anaSummaryList = new ArrayList<AnaSummaryStatEntity>();
		// 获取所有剧集信息
		List<SeriesStatEntity> seriesList = seriesStatService.getSelectAllList();

		// 昨日排行：上一日（0:00-23:59）的剧集点播次数排行；
		// 昨日点播：上一日（0:00-23:59）的剧集点播次数；
		List<AnaSummaryStatEntity> yesterdayWas = playStatisticsService.yesterdayWas();

		// 上周点播：当前时间的上一自然周（周一－周日）的点播次数；
		// 上周排行：当前时间的上一自然周（周一－周日）的点播次数排行；
		List<AnaSummaryStatEntity> lastWeekRanking = playStatisticsService.lastWeekRanking();

		// 截止到上周末点播
		// 截止到上周末排行
		List<AnaSummaryStatEntity> lastWeekClose = playStatisticsService.lastWeekClose();

		// 上月点播：当前时间的上一自然月（1- 30号）的点播次数；
		// 上月排行：当前时间的上一自然月（1-30号）的点播次数排行；
		List<AnaSummaryStatEntity> LastMonthRanking = playStatisticsService.LastMonthRanking();

		// 截止到上月点播
		// 截止到上月排行
		List<AnaSummaryStatEntity> LastMonthClose = playStatisticsService.LastMonthClose();

		// 总点播：截止到昨日（23:59）的点播总次数；
		// 总排行：截止到昨日（23:59）的点播排行；
		List<AnaSummaryStatEntity> yesterdayWasClose = playStatisticsService.yesterdayWasClose();

		// 日上升指数：＝前一日排行－昨日排行
		List<AnaSummaryStatEntity> dayUpIndex = playStatisticsService.dayUpIndex();

		// 周上升指数（最近一周）＝当前8日前排行－总排行
		List<AnaSummaryStatEntity> WeekUpIndex = playStatisticsService.WeekUpIndex();

		// 月上升指数：＝截止到上月末－总排行
		List<AnaSummaryStatEntity> MonthUpIndex = playStatisticsService.MonthUpIndex();

		// 将查询的所有不同信息放在一起插入anaSummary实体
		AnaSummaryStatEntity anaSummary;
		for (SeriesStatEntity sse : seriesList) {
			if (sse.getId() != null) {
				anaSummary = new AnaSummaryStatEntity();
				int id = sse.getId();
				anaSummary.setSeriesId(id);// 剧集Id
				anaSummary.setSeriesName(sse.getName()); // 剧集名称
				anaSummary.setSeriesKindCode(sse.getKind()); // 剧集分类编码
				anaSummary.setSeriesKindName(sse.getKindName());// 剧集分类名称
				anaSummary.setDramaTypeCode(sse.getType());// 剧情分类编码
				anaSummary.setDramaTypeName(sse.getTypeName());// 剧情分类名称
				// 昨日排名和次数
				for (AnaSummaryStatEntity yesterday : yesterdayWas) {
					int id2 = yesterday.getSeriesId();
					if (id == id2) {
						anaSummary.setLastdayRank(yesterday.getLastdayRank());
						anaSummary.setLastdayPlayCount(yesterday.getLastdayPlayCount());
						break;
					} else {

						continue;
					}

				}
				// 上一个自然周的排名和次数
				for (AnaSummaryStatEntity lastWeek : lastWeekRanking) {
					int id2 = lastWeek.getSeriesId();
					if (id == id2) {
						anaSummary.setLastweekRank(lastWeek.getLastweekRank());
						anaSummary.setLastweekPlayCount(lastWeek.getLastweekPlayCount());
						break;
					} else {

						continue;
					}

				}
				// 上一个自然月的排名和次数
				for (AnaSummaryStatEntity LastMonth : LastMonthRanking) {
					int id2 = LastMonth.getSeriesId();
					if (id == id2) {
						anaSummary.setLastmonthRank(LastMonth.getLastmonthRank());
						anaSummary.setLastmonthPlayCount(LastMonth.getLastmonthPlayCount());
						break;
					} else {
						continue;
					}

				}

				// 截止到上周点播排名和次数
				for (AnaSummaryStatEntity lastWeekc : lastWeekClose) {
					int id2 =0;
					if(lastWeekc.getSeriesId()!=null){
					id2= lastWeekc.getSeriesId();
					}
					if (id == id2) {
						anaSummary.setLastweekEndRank(lastWeekc.getLastweekEndRank());
						anaSummary.setLastweekEndPlayCount(lastWeekc.getLastweekEndPlayCount());
						break;
					} else {
						continue;
					}

				}

				// 截止到上月点播排行和次数
				for (AnaSummaryStatEntity lastMonthc : LastMonthClose) {
					int id2 = lastMonthc.getSeriesId();
					if (id == id2) {
						anaSummary.setLastmonthEndRank(lastMonthc.getLastmonthEndRank());
						anaSummary.setLastmonthEndPlayCount(lastMonthc.getLastmonthEndPlayCount());
						break;
					} else {
						continue;
					}

				}

				// 截止到昨日点播排行和次数 (总)
				for (AnaSummaryStatEntity yesterdayc : yesterdayWasClose) {
					int id2 =0;
					if(yesterdayc.getSeriesId()!=null){
					id2= yesterdayc.getSeriesId();
					}
					if (id == id2) {
						anaSummary.setLastdayEndRank(yesterdayc.getLastdayEndRank());
						anaSummary.setLastdayEndPlayCount(yesterdayc.getLastdayEndPlayCount());
						break;
					} else {
						continue;
					}

				}

				// 日上升指数
				for (AnaSummaryStatEntity dayUp : dayUpIndex) {
					int id2 = dayUp.getSeriesId();
					if (id == id2) {
						anaSummary.setDayGoupExp(dayUp.getDayGoupExp());//// 日上升指数
						break;
					} else {
						continue;
					}

				}
				// 周上升指数
				for (AnaSummaryStatEntity weekUp : WeekUpIndex) {
					int id2 =0;
					if(weekUp.getSeriesId()!=null){
					id2= weekUp.getSeriesId();
					}
					if (id == id2) {
						anaSummary.setWeekGoupExp(weekUp.getWeekGoupExp());//// 周上升指数
						break;
					} else {
						continue;
					}

				}

				// 月上升指数
				for (AnaSummaryStatEntity monthUp : MonthUpIndex) {
					int id2 =0;
					if(monthUp.getSeriesId()!=null){
					id2= monthUp.getSeriesId();
					}
					if (id == id2) {
						anaSummary.setMonthGoupExp(monthUp.getMonthGoupExp());//// 月上升指数
						break;
					} else {
						continue;
					}

				}

				// 将每个 查询的所有不同信息放在一起插入anaSummaryList集合
				anaSummaryList.add(anaSummary);
			}

		}

		// TODO Auto-generated method stub
		return anaSummaryList;
	}

	@Override
	public int addAnaSummaryStat() {
		// TODO Auto-generated method stub
		// 将查到的昨天所有剧集的各类信息放入anaSummaryList集合准备插入数据库
		List<AnaSummaryStatEntity> anaSummaryList = this.statAnaSummaryList();
		int i = 0;
		for (AnaSummaryStatEntity ase : anaSummaryList) {
			int c = 0;
			if (ase != null) {
				c = anaSummaryStatMapper.addAnaSummaryStat(ase);
				if (c != 0) {

					i++;
				}
			}

		}
		if (i == anaSummaryList.size()) {

			return 1;
		} else {
			return 0;

		}

	}

	// 根据参数查询不同的排名信息
	@Override
	public List<AnaSummaryStatEntity> selectAnaSummaryStatAll(AnaSummaryStatEntity anaSummaryStat) {
		// TODO Auto-generated method stub
		return anaSummaryStatMapper.selectAnaSummaryStatAll(anaSummaryStat);
	}

	// 根据日周月统计上升指数排名
	@Override
	public List<AnaSummaryStatEntity> selectAnaRisingIndexAll(AnaSummaryStatEntity anaSummaryStat) {
		// TODO Auto-generated method stub
		List<AnaSummaryStatEntity> anaSummaryList = new ArrayList<AnaSummaryStatEntity>();

		anaSummaryList = anaSummaryStatMapper.selectAnaRisingIndexAll(anaSummaryStat);
		// 将获取到的信息附加排名位次
		int i = 1;
		for (AnaSummaryStatEntity ass : anaSummaryList) {
			ass.setRankings(i);
			i++;

		}
		return anaSummaryList;
	}

	// 将每日的统计数据存储起来之前先删除旧的数据
	@Override
	public int deleteAnaSummaryStat() {
		// TODO Auto-generated method stub
		return anaSummaryStatMapper.deleteAnaSummaryStat();
	}

}
